﻿using System.Collections;

namespace BullsEyeGame
{
    /// <summary>
    /// This class will hold all the game logics
    /// </summary>
    internal class GameLogics
    {
        private const int k_MinNumberOfGuesses = 4;
        private const int k_MaxNumberOfGuesses = 10;
        
        private static GameLogics s_Instance = null;
        
        private GameData m_GameData = null;
        
        /// <summary>
        /// A static constructor
        /// </summary>
        static GameLogics()
        {
            s_Instance = new GameLogics();
        }

        /// <summary>
        /// property to get the minimum number of allowed Guesses
        /// </summary>
        public static int MinNumberOfGuesses
        {
            get { return k_MinNumberOfGuesses; }
        }

        /// <summary>
        /// property to get the maximum number of allowed Guesses
        /// </summary>
        public static int MaxNumberOfGuesses
        {
            get { return k_MaxNumberOfGuesses; }
        }               

        /// <summary>
        /// Will return the instance of this single-ton class
        /// </summary>
        public static GameLogics Instance
        {
            get { return s_Instance; }
        }

        /// <summary>
        /// A property to get the number of Guesses chosen by the user
        /// </summary>
        public int MaxNumberOfGuessesChosen
        {
            get { return (int)m_GameData.MaxNumberOfCuessesChosen; }
        }   

        /// <summary>
        /// Will return if the input is a valid number of Guesses
        /// </summary>
        /// <param name="i_NumberOfGuesses">The number of Guesses</param>
        /// <returns>
        /// true if the input is in the range of the maximum number of allowed Guesses
        /// and minimum number of allowed Guesses
        /// </returns>
        public static bool IsValidNumberOfGuesses(int i_NumberOfGuesses)
        {
            return (MinNumberOfGuesses <= i_NumberOfGuesses) && (i_NumberOfGuesses <= MaxNumberOfGuesses);
        }

        /// <summary>
        /// This is the initialization function of the new game. The function will set the 
        /// number of Guesses for this game and create a new target guess 
        /// </summary>
        /// <param name="i_NumberOfRounds">The number of guesses selected by the user</param>
        public void StartNewGame(int i_NumberOfRounds)
        {
            m_GameData = GameData.CreateGameData(i_NumberOfRounds);
        }       

        /// <summary>
        /// A property to get the target Guess
        /// </summary>
        public Guess TargetGuess
        {
            get { return (m_GameData != null) ? m_GameData.TagetGuess : null; }
        }
        
        /// <summary>
        /// A property to get the array of GuessResult
        /// </summary>
        public ArrayList GuessesResults
        {
            get { return m_GameData.GuessesResults; }
        }

        /// <summary>
        /// Will create a GuessResult from the input, add the result to GameData local GueastResult array
        /// </summary>
        /// <param name="i_NewGuess">A User Guess. 
        /// </param>
        /// <returns>The GuessResult for this input. If GameData for this game was not set then null will be returned</returns>
        /// <returns></returns>
        public GuessResult AddNewGuess(Guess i_NewGuess)
        {
            return (m_GameData != null) ? m_GameData.AddNewGuess(i_NewGuess) : null; 
        }

        private GameLogics()
        {
        }
    }
}
